package com.example.s.Activity.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.s.Activity.BaseActivity;
import com.example.s.R;
import com.example.s.view.MyFullVideoView;

public class SplashActivity extends BaseActivity {
    private MyFullVideoView vv;
    private TextView title;
    private SharedPreferences sharedPreferences;

    private void initView() {

        vv = (MyFullVideoView) findViewById(R.id.vv);
        title = (TextView) findViewById(R.id.title);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        sharedPreferences = getSharedPreferences("light", MODE_PRIVATE);
        final String[] titleArr = new String[]{
                "无人做你的光芒，就自己照亮远方。",
                "可以不光芒万丈，但不能停止自己发光。",
                "满天繁星有我想成为的那道光。",
                "发光并非太阳的专利，你也可以发光。",
                "你相信光吗？",
                "你不用去追逐光，因为总有一天你也会变成光",
                "某一个，你的光照亮了我",
                "仰慕光 靠近光 成为光 散发光",
                "我可以成为你的光吗？"

        };
        initView();
        final String uri = "android.resource://" + getPackageName() + "/" + R.raw.light;
        Log.d(TAG, "onCreate: "+uri);
        vv.setVideoURI(Uri.parse(uri));
        vv.start();
        vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                int welcomeIndex = sharedPreferences.getInt("welcomeIndex", 0);
                title.setText(titleArr[welcomeIndex % titleArr.length]);
                sharedPreferences.edit().putInt("welcomeIndex", ++welcomeIndex).apply();
                mp.start();
                mp.setLooping(false);


            }
        });


        vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                boolean isFirst = sharedPreferences.getBoolean("isFirst", true);
                if (isFirst) {
                    startActivity(new Intent(SplashActivity.this, GuideActivity.class));
                    sharedPreferences.edit().putBoolean("isFirst",false).commit();
                    return;
                }
                startActivity(new Intent(SplashActivity.this, LoginOrRegisterActivity.class));
                finish();
            }
        });
    }


}
